// Project: dip_meter
// FileName: dip_meter
#include <p18f14k50.h>
#include <delays.h>
#include <timers.h>
#include "lib_adc.h"
#pragma config CPUDIV = NOCLKDIV
#pragma config USBDIV = OFF
#pragma config FOSC = HS
#pragma config PLLEN = ON
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRTEN = OFF
#pragma config BOREN = OFF
#pragma config BORV = 30
#pragma config WDTEN = OFF
#pragma config WDTPS = 32768
#pragma config MCLRE = OFF
#pragma config HFOFST = OFF
#pragma config STVREN = ON
#pragma config LVP = OFF
#pragma config XINST = OFF
#pragma config BBSIZ = OFF
#pragma config CP0 = OFF
#pragma config CP1 = OFF
#pragma config CPB = OFF
#pragma config WRT0 = OFF
#pragma config WRT1 = OFF
#pragma config WRTB = OFF
#pragma config WRTC = OFF
#pragma config EBTR0 = OFF
#pragma config EBTR1 = OFF
#pragma config EBTRB = OFF
#define lcd_DB LATB
#define lcd_E LATCbits.LATC7
#define lcd_RS LATCbits.LATC3
#define Delay_50uS Delay100TCYx(6)
#define Delay_1mS Delay1KTCYx(12)
#define Delay_2mS Delay1KTCYx(24)
#define Delay_5mS Delay10KTCYx(6)
#define Delay_10mS Delay10KTCYx(12)
#define Delay_20mS Delay10KTCYx(24)
#define Delay_100mS Delay10KTCYx(120)
unsigned long FFF, FF;
unsigned long A, TL;
unsigned long v;
unsigned char N;
void lcd_w4(char asci);
void lcd_c4(char cmd);
void lcd_clr(void);
void lcd_init(void);
void lcd_puts(char *s);
void lcd_locate(char y,char x);
void lcd_putf(unsigned long f);
void UserInit(void);
void lcd_Init(void);
void high(void);
void low(void);
#pragma interrupt high
#pragma interruptlow low
#pragma code H_vect = 0x0008
void H_isr(void){
_asm goto high _endasm
}
#pragma code L_vect = 0x0018
void L_isr(void){
_asm goto low _endasm
}
#pragma code
void high(){
if(INTCONbits.TMR0IF){
A++;
INTCONbits.TMR0IF = 0;
}
}
void low(){
if(PIR1bits.TMR1IF){
N++;
PIR1bits.TMR1IF = 0;
if(N > 23){
T0CONbits.TMR0ON = 0;
Delay1TCY();
N = 0; TL = TMR0L;
FFF = A*256 + TL;
A = 0; TMR0L = 0;
T0CONbits.TMR0ON = 1;
}
TMR1H = 11; TMR1L = 236;
}
}
void lcd_w4(char c){
lcd_DB = (c & 0xF0) | (lcd_DB & 0x0F);
lcd_E = 1;
Delay10TCYx(20);
lcd_E = 0;
}
void lcd_c4(char c){
lcd_RS = 0;
lcd_w4(c);
lcd_w4(c<<4);
if(c & 0xFC)
Delay_50uS;
else
Delay_2mS;
}
void lcd_clr(void){
lcd_c4(0x01);
}
void lcd_locate(char y, char x){
unsigned char p;
switch(y & 0x03){
case 0: p=0x80; break;
case 1: p=0xC0; break;
case 2: p=0x94; break;
case 3: p=0xD4; break;
}
lcd_c4(p += x);
}
void lcd_putchr(char c){
lcd_RS = 1;
lcd_w4(c);
lcd_w4(c<<4);
Delay_50uS;
}
void lcd_puts(char *s){
while(*s)
lcd_putchr(*s++);
}
void lcd_putf(
unsigned long f){
unsigned char i;
unsigned char buf[8];
f = f * 64;
for(i=0; i<9; i++) buf[i] = ' ';
i = 8;
do{
buf[i] = (f % 10) + '0';
f = f / 10;
i--;
} while(f > 0);
for(i=0; i<3; i++) lcd_putchr(buf[i]);
lcd_putchr('.');
for(i=3; i<7; i++) lcd_putchr(buf[i]);
}
void lcd_putui(
unsigned long ui, unsigned char d){
unsigned char i;
unsigned char buf[7];
for(i = 0; i < 7; i++)
buf[i] = ' ';
i = 5;
do {
buf[i] = (ui % 10) + '0';
ui = ui / 10;
i--;
} while(ui > 0);
for(i = (7 - d); i < 7; i++)
lcd_putchr(buf[i]);
}
void lcd_init(void){
Delay_20mS;
lcd_RS = 0;
lcd_w4(0x30);Delay_5mS;
lcd_w4(0x30);Delay_1mS;
lcd_w4(0x30);Delay_1mS;
lcd_w4(0x20);Delay_1mS;
lcd_c4(0x2E);
lcd_c4(0x08);
lcd_c4(0x0C);
lcd_c4(0x06);
lcd_c4(0x01);
}
void UserInit(void){
N = 0; A = 0;
// FF = 0;
}
void main(void){
PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
TRISA = 0b00000000;
TRISB = 0b00000000;
TRISC = 0b01100000;
ANSEL = 0b00000000;
ANSELH = 0b00000001;
OpenTimer0(
TIMER_INT_ON &
T0_8BIT &
T0_PS_1_1 &
T0_SOURCE_EXT &
T0_EDGE_RISE
);
INTCON2bits.TMR0IP = 1;
INTCONbits.TMR0IF = 0;
OpenTimer1(
TIMER_INT_ON &
T1_8BIT_RW &
T1_SOURCE_INT &
T1_PS_1_8 &
T1_OSC1EN_OFF &
T1_SYNC_EXT_OFF
);
IPR1bits.TMR1IP = 0;
PIR1bits.TMR1IF = 0;
RCONbits.IPEN = 1;
INTCONbits.GIEH = 1;
INTCONbits.GIEL = 1;
TMR0H = 0; TMR0L = 0;
TMR1L = 10;
UserInit();
lcd_init();
// LCDに 外字登録 0x01:| 0x02:|| 0x03:|||
lcd_c4(0x48); lcd_putchr(0x10);
lcd_c4(0x49); lcd_putchr(0x10);
lcd_c4(0x4A); lcd_putchr(0x10);
lcd_c4(0x4B); lcd_putchr(0x10);
lcd_c4(0x4C); lcd_putchr(0x10);
lcd_c4(0x4D); lcd_putchr(0x10);
lcd_c4(0x4E); lcd_putchr(0x00);
lcd_c4(0x4F); lcd_putchr(0x00);
lcd_c4(0x50); lcd_putchr(0x14);
lcd_c4(0x51); lcd_putchr(0x14);
lcd_c4(0x52); lcd_putchr(0x14);
lcd_c4(0x53); lcd_putchr(0x14);
lcd_c4(0x54); lcd_putchr(0x14);
lcd_c4(0x55); lcd_putchr(0x14);
lcd_c4(0x56); lcd_putchr(0x00);
lcd_c4(0x57); lcd_putchr(0x00);
lcd_c4(0x58); lcd_putchr(0x15);
lcd_c4(0x59); lcd_putchr(0x15);
lcd_c4(0x5A); lcd_putchr(0x15);
lcd_c4(0x5B); lcd_putchr(0x15);
lcd_c4(0x5C); lcd_putchr(0x15);
lcd_c4(0x5D); lcd_putchr(0x15);
lcd_c4(0x5E); lcd_putchr(0x00);
lcd_c4(0x5F); lcd_putchr(0x00);
while(1){
//if(FF != FFF){
//FF = FFF;
lcd_clr();
lcd_locate(0, 0);
OpenADC(ADC_FOSC_16 &
ADC_RIGHT_JUST &
ADC_12_TAD,
ADC_REF_VDD_VDD &
ADC_REF_VDD_VSS,
ADC_CH8 &
ADC_INT_OFF );
ConvertADC();
while(BusyADC());
v=(ADRESH*256) + ADRESL;
// バーグラフ表示
if((200 < v) && (v < 217)){
lcd_putchr(0x01);
}
if((216 < v) && (v < 234)){
lcd_putchr(0x02);
}
if((233 < v) && (v < 251)){
lcd_putchr(0x03);
}
if((250 < v) && (v < 268)){
lcd_putchr(0x03); lcd_putchr(0x01);
}
if((267 < v) && (v < 285)){
lcd_putchr(0x03); lcd_putchr(0x02);
}
if((284 < v) && (v < 302)){
lcd_putchr(0x03); lcd_putchr(0x03);
}
if((301 < v) && (v < 319)){
lcd_putchr(0x03); lcd_putchr(0x03); lcd_putchr(0x01);
}
if((318 < v) && (v < 336)){
lcd_putchr(0x03); lcd_putchr(0x03); lcd_putchr(0x02);
}
if((335 < v) && (v < 353)){
lcd_putchr(0x03); lcd_putchr(0x03); lcd_putchr(0x03);
}
if((352 < v) && (v < 370)){
lcd_putchr(0x03); lcd_putchr(0x03); lcd_putchr(0x03);
lcd_putchr(0x01);
}
if((369 < v) && (v < 387)){
lcd_putchr(0x03); lcd_putchr(0x03); lcd_putchr(0x03);
lcd_putchr(0x02);
}
if((386 < v) && (v < 404)){
lcd_putchr(0x03); lcd_putchr(0x03); lcd_putchr(0x03);
lcd_putchr(0x03);
}
if((403 < v) && (v < 421)){
lcd_putchr(0x03); lcd_putchr(0x03); lcd_putchr(0x03);
lcd_putchr(0x03); lcd_putchr(0x01);
}
if((420 < v) && (v < 438)){
lcd_putchr(0x03); lcd_putchr(0x03); lcd_putchr(0x03);
lcd_putchr(0x03); lcd_putchr(0x02);
}
if((437 < v) && (v < 455)){
lcd_putchr(0x03); lcd_putchr(0x03); lcd_putchr(0x03);
lcd_putchr(0x03); lcd_putchr(0x03);
}
if((454 < v) && (v < 472)){
lcd_putchr(0x03); lcd_putchr(0x03); lcd_putchr(0x03);
lcd_putchr(0x03); lcd_putchr(0x03); lcd_putchr(0x01);
}
if((471 < v) && (v < 489)){
lcd_putchr(0x03); lcd_putchr(0x03); lcd_putchr(0x03);
lcd_putchr(0x03); lcd_putchr(0x03); lcd_putchr(0x02);
}
if((488 < v) && (v < 506)){
lcd_putchr(0x03); lcd_putchr(0x03); lcd_putchr(0x03);
lcd_putchr(0x03); lcd_putchr(0x03); lcd_putchr(0x03);
}
if((505 < v) && (v < 523)){
lcd_putchr(0x03); lcd_putchr(0x03); lcd_putchr(0x03);
lcd_putchr(0x03); lcd_putchr(0x03); lcd_putchr(0x03);
lcd_putchr(0x01);
}
if((522 < v) && (v < 540)){
lcd_putchr(0x03); lcd_putchr(0x03); lcd_putchr(0x03);
lcd_putchr(0x03); lcd_putchr(0x03); lcd_putchr(0x03);
lcd_putchr(0x02);
}
if((539 < v) && (v < 557)){
lcd_putchr(0x03); lcd_putchr(0x03); lcd_putchr(0x03);
lcd_putchr(0x03); lcd_putchr(0x03); lcd_putchr(0x03);
lcd_putchr(0x03);
}
if((556 < v) && (v < 574)){
lcd_putchr(0x03); lcd_putchr(0x03); lcd_putchr(0x03);
lcd_putchr(0x03); lcd_putchr(0x03); lcd_putchr(0x03);
lcd_putchr(0x03); lcd_putchr(0x01);
}
if((573 < v) && (v < 591)){
lcd_putchr(0x03); lcd_putchr(0x03); lcd_putchr(0x03);
lcd_putchr(0x03); lcd_putchr(0x03); lcd_putchr(0x03);
lcd_putchr(0x03); lcd_putchr(0x02);
}
if(590 < v){
lcd_putchr(0x03); lcd_putchr(0x03); lcd_putchr(0x03);
lcd_putchr(0x03); lcd_putchr(0x03); lcd_putchr(0x03);
lcd_putchr(0x03); lcd_putchr(0x03);
}
lcd_locate(1, 0);
lcd_putf(FFF);
//}
Delay_100mS;
Delay_100mS;
}
}